#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project: Jmpy
@File   : jmp_plot_clustering.py
@Author : Link
@Date   : 2025/5/1 12:57
@Remark : 聚类相关

"""
from string import Template


class JmpyPlotClustering:

    @staticmethod
    def jWaferCluster(
            name: str, save_path: str, group: str, da_group: str, obj: str = "obj"
    ):
        """
        将聚类结果的数据保存到SQL中
        1. 代码用
        :return:
        """

        return Template("""
$obj << Save Clusters;
s_dt = Data Table( "$name FAIL_FLAG 空间" ) << Subset(
    All rows,s
    columns( :$group, :$da_group, :Object, :Cluster )
); 
s_dt << Save( "$save_path" ); // convert to CSV format
Close( s_dt, "NoSave" );
                """).substitute(**locals())

    @staticmethod
    def jWaferClustering(x_coord: str, y_coord: str, data: str, group: str, da_group: str,
                         obj: str = "obj", title: str = "Title"):
        """
        传入列名
        :return:
        """
        return Template("""
$obj = Hierarchical Cluster(
    Y( :$data ),
    Object ID( :$group, :$da_group ),
    Attribute ID( :$x_coord, :$y_coord ),
    Method( "Ward" ),
    Standardize Data( 0 ),
    Cluster Summary( 1 ),
    Dendrogram Scale( "Distance Scale" ),
    Number of Clusters( 20 ),
    Add Spatial Measures(
        Attributes( 1 ),
        Angle( 1 ),
        Radius( 1 ),
        Streak Angle( 1 ),
        Streak Distance( 1 )
    ),
    SendToReport(
        Dispatch( {}, "Dendrogram", OutlineBox, {Close( 1 ), SetHorizontal( 1 )} ),
        Dispatch(
            {"Dendrogram"},
            "Clust Dendro",
            FrameBox,
            {Frame Size( 35, 700 )}
        )
    )
);
$obj << Title( "$title" );
        """).substitute(**locals())
